﻿using Avalonia.Controls;
using Avalonia.Exusiai.Editor.Models;
using Avalonia.Exusiai.Editor.ViewModels;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.ReactiveUI;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Avalonia.Exusiai.Editor.Views;

public partial class ObjectInspectionView : ReactiveUserControl<ObjectInspectionViewModel>
{
    public ObjectInspectionView()
    {
        InitializeComponent();
        DataContext = new ObjectInspectionViewModel();
    }
    #region Renderer
    private void Material_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox != null)
        {
            ObjectInspectionViewModel.MaterialPath = textBox.Text;
        }
    }

    private void Material_OnKeyUp(object? sender, KeyEventArgs e)
    {
        var textBox = sender as TextBox;
        if (e.Key == Key.Enter && textBox != null)
        {
            ObjectInspectionViewModel.MaterialPath = textBox.Text;
        }
    }
    private void Model_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox != null)
        {
            ObjectInspectionViewModel.ModelPath = textBox.Text;
        }
    }

    private void Model_OnKeyUp(object? sender, KeyEventArgs e)
    {
        var textBox = sender as TextBox;
        if (e.Key == Key.Enter && textBox != null)
        {
            ObjectInspectionViewModel.ModelPath = textBox.Text;
        }
    }
    #endregion
    #region NumberChange
    private void NumericUpDown_OnValueChanged(object? sender, NumericUpDownValueChangedEventArgs e)
    {
        if (sender is NumericUpDown numericUpDown)
        {
            if (e.NewValue.ToString() == "" || !float.TryParse(e.NewValue.ToString(), out _))
            {
                // 值被删除或输入无效时，可以在这里添加代码来处理错误或恢复到之前的值
                numericUpDown.Value = 0;
            }
        }
    }
    #endregion
    #region Name
    private void InputElement_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox != null)
        {
            ObjectInspectionViewModel.Name = textBox.Text;
        }
    }

    private void InputElement_OnKeyUp(object? sender, KeyEventArgs e)
    {
        var textBox = sender as TextBox;
        if (e.Key == Key.Enter && textBox != null)
        {
            ObjectInspectionViewModel.Name = textBox.Text;
        }
    }
    #endregion
    #region Position
    private void PositionX_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if(num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.PositionX = (float)num.Value;
        }
    }

    private void PositionX_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if(e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.PositionX = (float)num.Value;
            }
        }
    }
    private void PositionY_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.PositionY = (float)num.Value;
        }
    }

    private void PositionY_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.PositionY = (float)num.Value;
            }
        }
    }
    private void PositionZ_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.PositionZ = (float)num.Value;
        }
    }

    private void PositionZ_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.PositionZ = (float)num.Value;
            }
        }
    }
    #endregion
    #region Rotation

    private void RotationX_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.RotationX = (float)num.Value;
        }
    }

    private void RotationX_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.RotationX = (float)num.Value;
            }
        }
    }
    private void RotationY_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.RotationY = (float)num.Value;
        }
    }

    private void RotationY_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.RotationY = (float)num.Value;
            }
        }
    }
    private void RotationZ_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.RotationZ = (float)num.Value;
        }
    }

    private void RotationZ_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.RotationZ = (float)num.Value;
            }
        }
    }
    #endregion
    #region Scale

    private void ScaleX_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.ScaleX = (float)num.Value;
        }
    }

    private void ScaleX_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.ScaleX = (float)num.Value;
            }
        }
    }
    private void ScaleY_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.ScaleY = (float)num.Value;
        }
    }

    private void ScaleY_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.ScaleY = (float)num.Value;
            }
        }
    }
    private void ScaleZ_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var num = sender as NumericUpDown;
        if (num != null)
        {
            if (num.Value == null) num.Value = 0;
            ObjectInspectionViewModel.ScaleZ = (float)num.Value;
        }
    }

    private void ScaleZ_OnKeyUp(object? sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            var num = sender as NumericUpDown;
            if (num != null)
            {
                if (num.Value == null) num.Value = 0;
                ObjectInspectionViewModel.ScaleZ = (float)num.Value;
            }
        }
    }
    #endregion
}